package com.usefullc.blog.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usefullc.common.util.BeanUtils;
import com.usefullc.blog.domain.Reply;
import com.usefullc.blog.mapper.ReplyMapper;
import com.usefullc.blog.query.ReplyQuery;
import com.usefullc.blog.vo.ReplyVo;
import com.usefullc.system.entity.Pager;
import com.usefullc.system.service.AbstractBizService;
import com.usefullc.user.domain.User;
import com.usefullc.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 评论服务类
 * @author: 星空
 * @date: 2022-07-07
 */
@Service
public class ReplyService extends AbstractBizService<ReplyMapper, Reply, ReplyQuery> {


    @Autowired
    private UserService userService;

    @Override
    protected QueryWrapper<Reply> getQueryWrapper(ReplyQuery query) {
        QueryWrapper<Reply> queryWrapper = super.getQueryWrapper(query);
        return queryWrapper;
    }

    public Pager<ReplyVo> findVoByPage(ReplyQuery query){
        Pager<Reply> page = this.findByPage(query);
        Pager<ReplyVo> newPage = new Pager<>(page);
        List<Reply> dataList = page.getDataList();
        List<ReplyVo> newDataList = new ArrayList<>();
        newPage.setDataList(newDataList);
        for (Reply reply : dataList) {
            ReplyVo replyVo = BeanUtils.beanCopy(reply, ReplyVo.class);

            User user = userService.findById(reply.getUserId());
            replyVo.setReplyUser(user);
            newDataList.add(replyVo);
        }
        return newPage;
    }


}
